import React from 'react'
import store from '../../store/store'


export default function Count() {
  // 这个放上面,因为下面要判断count
  const { count } = store.getState()


  //这边向store发送信息后,store给reducer处理,reducer在里解构接收
  //累加
    const incrementHandle = () => {
      // 点击时向store发送信息
      store.dispatch({
        type:'incrementHandle',
        data:1
      })
    }
  
    const decreamentHandle = () => {
      //累减
      store.dispatch({
        type:'decreamentHandle',
        data:1
      })
  
    }
  
    const ifOddDecrementHandle = () => {
      //奇数加一
      // 判断2%不等于0,就累加
      if(count%2 !== 0 ){
        store.dispatch({
          type:'incrementHandle',
          data:1
        })
      }
    }
  
    const waitDecrementHandle = () => {
      // 定时器累加
      setTimeout(()=>{
        store.dispatch({
          type:'incrementHandle',
          data:1
        })
      },2000)
    }

    
  return (
    <div>
         <h1>我是Count组件 我的值是{count }</h1>
      <div>
        <button onClick={incrementHandle}>累加</button>
        <button onClick={decreamentHandle}>累减</button>
        <button onClick={ifOddDecrementHandle}>如果是奇数再加1</button>
        <button onClick={waitDecrementHandle}>等2s再加1</button>
      </div>
    </div>
  )
}
